package com.yyuap.standard.service;

import java.util.List;

import com.yonyou.um.json.JSONArray;
import com.yonyou.um.json.JSONObject;
import com.yonyou.um.plugins.persist.PatchDB;
import com.yonyou.um.webmvc.controller.UMController;
import com.yyuap.standard.impl.UserImpl;
import com.yyuap.standard.itf.IUser;
import com.yyuap.standard.vo.UserVO;

public class StandardController extends UMController {
/**
 *DB的 init()未执行
 */
	static {
		PatchDB.justForLoading();
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadData() throws Exception {

		String condition = this.getParaFromClient("condition").toString();

		JSONObject resultJson = new JSONObject();
		JSONArray stuArray = new JSONArray();
		String flag = "";
		String des = "";

		try {
			UserImpl iuser = new UserImpl();
			List<UserVO> users = iuser.findUser(condition);
			for (int i = 0; i < users.size(); i++) {
				JSONObject userObj = new JSONObject();
				UserVO user = users.get(i);

				// userObj.put("id", user.getId());
				// userObj.put("stuID", user.getStuid());
				// userObj.put("name", user.getName());
				// userObj.put("sex", user.getSex());
				// userObj.put("age", user.getAge());
				// userObj.put("score", user.getScore());
				userObj.put("id", user.get("id"));
				userObj.put("stuID", user.get("stuid"));
				userObj.put("name", user.get("name"));
				userObj.put("sex", user.get("sex"));
				userObj.put("age", user.get("age"));
				userObj.put("score", user.get("score"));

				stuArray.put(i, userObj);
				// stuArray.add(userObj);
			}
			resultJson.put("stuList", stuArray);
			flag = "success";
			des = "加载数据成功";

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "failed";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

	/**
	 * 增加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addData() throws Exception {
		Object userInfor = this.getParaFromClient("userInfor");

		JSONObject userObj = new JSONObject(userInfor.toString());
		String stuID = (String) userObj.get("stuID");
		String name = (String) userObj.get("name");
		String sex = (String) userObj.get("sex");
		int age = Integer.valueOf((String) userObj.get("age"));
		Float score = Float.parseFloat((String) userObj.get("score"));

		UserVO user = new UserVO();
		user.set("stuid", stuID);
		user.set("name", name);
		user.set("sex", sex);
		user.set("age", age);
		user.set("score", score);

		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";

		try {
			IUser iuser = new UserImpl();
			Boolean addFlag = iuser.insertUser(user);
			if (addFlag == true) {
				flag = "success";
				des = "添加数据成功";
			} else {
				flag = "failed";
				des = "添加数据失败";
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "failed";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */

	public String delData() throws Exception {
		String idStr = this.getParaFromClient("condition").toString();

		int id = Integer.parseInt(idStr);

		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";
		try {

			IUser iuser = new UserImpl();
			UserVO user = new UserVO();
			user.set("id", id);
			Boolean delFlag = iuser.deleteUser(user);
			if (delFlag == true) {
				flag = "success";
				des = "删除数据成功";
			} else {
				flag = "failed";
				des = "删除数据失败";
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "success";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();

	}

	/**
	 * 更新
	 * 
	 * @return
	 * @throws Exception
	 */

	public String updateData() throws Exception {
		String idStr = this.getParaFromClient("id").toString();
		int id = Integer.parseInt(idStr);

		Object userInfor = this.getParaFromClient("userInfor");
		JSONObject userObj = new JSONObject(userInfor.toString());
		String stuID = (String) userObj.get("stuID");
		String name = (String) userObj.get("name");
		String sex = (String) userObj.get("sex");
		int age = Integer.valueOf((String) userObj.get("age"));
		Float score = Float.parseFloat((String) userObj.get("score"));

		UserVO user = new UserVO();
		user.set("id", id);
		user.set("stuid", stuID);
		user.set("name", name);
		user.set("sex", sex);
		user.set("age", age);
		user.set("score", score);

		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";

		try {
			IUser iuser = new UserImpl();
			Boolean updateFlag = iuser.updateUser(user);

			if (updateFlag == true) {
				flag = "success";
				des = "更新数据成功";
			} else {
				flag = "failed";
				des = "更新数据失败";
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "failed";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

}
